package p.wechat.zwapi.util;

import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import p.wechat.zwapi.basis.WeChatApiFuncType;
import p.wechat.zwapi.config.WebSocketConfig;
import p.wechat.zwapi.dto.Message;

import java.util.Objects;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedTransferQueue;

/**
 * http跑龙套
 *
 * @author 33
 * @date 2020/05/08
 */
@Slf4j
public class WeChatHttpUtil {
    static final BlockingQueue<Message> robots = new LinkedTransferQueue<>();
    public static TimedCache<String, Long> stringLongTimedCache = new TimedCache<>(1000 * 60);


    public static JSONObject send(Message robot) {
        robot = JSONUtil.parseObj(JSONUtil.toJsonStr(robot)).toBean(Message.class);

        robots.offer(robot);
        return null;
    }

    public static JSONObject sendNotURL(Message robot) {
        robot = JSONUtil.parseObj(JSONUtil.toJsonStr(robot)).toBean(Message.class);
        robots.offer(robot);
        return null;
    }

    public static String exec(Message robot) {
        String str = JSONUtil.toJsonStr(robot);
        if (exist(robot.getFunid(), WeChatApiFuncType.发送图片.getValue(), WeChatApiFuncType.发送文件.getValue())) {
            str = str.replaceAll("\\\\\\\\", "/");
        }
        return exec(str);

    }

    /**
     * 存在
     *
     * @param p    p
     * @param list 列表
     * @return {@link Boolean}  当p存在于list中时返回true
     */
    public static Boolean exist(Object p, Object... list) {
        for (Object o : list) {
            if (Objects.equals(o, p)) {
                return true;
            }
        }
        return false;
    }


    public static String exec(JSONObject robot) {
        return exec(JSONUtil.toJsonStr(robot));
    }

    public static String exec(String str) {
        HttpRequest post = cn.hutool.http.HttpUtil.createPost(WebSocketConfig.HTTPURL);
        post.contentType("application/json; Charset=UTF-8");
        if (log.isDebugEnabled()) {
            log.debug("请求:{}", str);
        }
        post.body(str);
        String body = post.execute().body();
        if (log.isDebugEnabled()) {
            log.debug("响应:{}", body);
        }
        return body;

    }

    public static JSONObject execObj(Message robot) {
        return JSONUtil.parseObj(exec(robot));
    }


    public static JSONArray execJSONArr(Message robot) {
        return JSONUtil.parseArray(exec(robot));
    }


    public static void 启动队列() {
        ThreadUtil.execAsync(() -> {
            while (Thread.currentThread().isInterrupted() == false) {
                try {
                    Message take = robots.take();
                    ThreadUtil.sleep(1000);
                    exec(take);
                } catch (Exception e) {
                    log.error("请求错误", e);
                }
            }
            System.out.println("结束队列");
        });
    }






}
